// Template Provided By
// Code Endeavors, LLC - http://www.codeendeavors.com

using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Web.UI;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.UI.Utilities;

namespace $YourCompany$.Modules.$safeprojectname$
{
    //-------------------------------------------------------------------------------------------------------------
    //- The ControlMethodClass attribute identifies the class as supporting Control Methods
    //- The Friendly name parameter is usually named after server-side class namespace, but could be shortened if you like
    //- If you change this make sure you also change its client calling code found in Edit$safeprojectname$.ascx.js
    //-   dnn.xmlhttp.callControlMethod('$YourCompany$.Modules.$safeprojectname$.Edit$safeprojectname$.' + this._ns, 
    //-------------------------------------------------------------------------------------------------------------
    [ControlMethodClass("$YourCompany$.Modules.$safeprojectname$.Edit$safeprojectname$")]
    public partial class Edit$safeprojectname$ : AjaxPortalModuleBase
    {
        //hookup events
        override protected void OnInit(EventArgs e)
        {
            this.AddScriptComponentDescriptors += new AddScriptComponentDescriptorsDelegate(this.AjaxPortalModuleBase_AddScriptComponentDescriptors);
            this.AddScriptReferences += new AddScriptReferencesDelegate(this.AjaxPortalModuleBase_AddScriptReferences);
            this.AddLocalizedMessages += new AddLocalizedMessagesDelegate(this.AjaxPortalModuleBase_AddLocalizedMessages);
            base.OnInit(e);
        }

#region AjaxPortalModuleBase Events
        //-------------------------------------------------------------------------------------------------------------
        //- This is where you add your custom properties that are to be sent down to the client-side object
        //- By default we are passing the naming container ID (ns) and localized messages (msgs) 
        //- If you add your own properties make sure you update your client code with those properties found in Edit$safeprojectname$.ascx.js
        //-      get_ImagePath: function() {return this._imagePath;},
        //-      set_ImagePath: function(value) {this._imagePath = value;},
        //-------------------------------------------------------------------------------------------------------------
        protected void AjaxPortalModuleBase_AddScriptComponentDescriptors(ScriptComponentDescriptor Descriptor)
        {
            //IMPORTANT!  Enter Client Namespace + ObjectName as Type
            Descriptor.Type = "$YourCompany$.Edit$safeprojectname$";

            //---------------------------------------------------------------------------------------------------------
            //Add custom properties here
            //   Descriptor.AddScriptProperty("ImagePath", String.Format("'{0}'", Me.ModulePath + "images/"))

            if (Settings != null)
            {
                //This sample uses the Module Settings collection as a custom property serialized to JSON
                JavaScriptSerializer jss = new JavaScriptSerializer();
                Descriptor.AddScriptProperty("settings", jss.Serialize(Settings));
            }
            //---------------------------------------------------------------------------------------------------------
        }

        //-------------------------------------------------------------------------------------------------------------
        //- This is where your client-side javascript that uses the MS AJAX framework needs to be registered
        //- Adding the reference here ensures that the MS AJAX script is run before our script which uses things like Type.registerNamespace run
        //-------------------------------------------------------------------------------------------------------------
        protected void AjaxPortalModuleBase_AddScriptReferences(List<ScriptReference> References)
        {
            References.Add(new System.Web.UI.ScriptReference(this.ModulePath + "Edit$safeprojectname$.ascx.js"));
        }

        //-------------------------------------------------------------------------------------------------------------
        //- Add any localized text needed on the client
        //-------------------------------------------------------------------------------------------------------------
        protected void AjaxPortalModuleBase_AddLocalizedMessages(Dictionary<string, string> Messages)
        {
            Messages["SettingsSaved"] = Localization.GetString("SettingsSaved.Client", LocalResourceFile);
        }

#endregion

#region Event Handlers
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                cmdCancel.NavigateUrl = DotNetNuke.Common.Globals.NavigateURL();
            }
            catch (Exception exc)         //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }
#endregion


#region "Control Methods"
        //-------------------------------------------------------------------------------------------------------------
        //- Exposing methods to the client code is as simple as adding the ControlMethod attribute
        //- IMPORTANT!   ALWAYS SECURE YOUR CONTROL METHODS IF INTERACTING WITH SENSITIVE DATA
        //-------------------------------------------------------------------------------------------------------------
        [ControlMethod]
        public bool UpdateSettings(Dictionary<string, object> Settings)
        {
            if (this.EditMode) //Making sure only those users with Edit Mode rights can call this method
            {
                ModuleController modController = new ModuleController();
                foreach (string key in Settings.Keys)
                {
                    modController.UpdateModuleSetting(this.ModuleId, key, Settings[key].ToString());
                }
            }
            return true;
        }
#endregion

    }
}